package edu.cibertec.sicsolutions.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Clase utilitaria para manejo de Fechas
 *  
 */
public final class DateUtil {
	
	private DateUtil(){
	}

	/** Convierte <code>java.util.Date</code> a String con un formato, por ejemplo "yyyyMMdd"
	 * @param date
	 * @param format
	 * @return
	 */
	public static String dateToString(Date date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);		
		return sdf.format(date);
	}
	
	/**
	 * Convierte <code>java.sql.Timestamp</code> a String con un formato, por ejemplo "yyyyMMdd"
	 * @param timestamp
	 * @param format
	 * @return
	 */
	public static String timestampToString(Timestamp timestamp, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);		
		return sdf.format(timestamp);
	}
	
    /**
     * Convierte <code>Timestamp</code> a <code>java.util.Date</code>
     * @param ts
     * @return
     */
    public static Date timestampToJavaUtilDate(Timestamp ts) {
		return new Date(ts.getTime());
	}

	/**
	 * Convierte <code>java.sql.Date</code> a <code>Timestamp</code>
	 * @param dt
	 * @return
	 */
	public static Timestamp javaSqlDateToTimestamp(java.sql.Date dt) {
		return new Timestamp(dt.getTime());
	}
	
    /**
     * Convierte <code>Timestamp</code> a <code>java.sql.Date</code>
     * @param ts
     * @return
     */
    public static java.sql.Date timestampToJavaSqlDate(Timestamp ts) {
		return new java.sql.Date(ts.getTime());
	}

	/**
	 * Convierte <code>java.util.Date</code> a <code>Timestamp</code>
	 * @param dt
	 * @return
	 */
	public static Timestamp javaUtilDateToTimestamp(Date dt) {
		return new Timestamp(dt.getTime());
	}

	/**
	 * Adiciona n dias a la fecha
	 * 
	 * @param timestamp
	 * @param days
	 * @return
	 * @throws ParseException
	 */
	public static Timestamp timestampAddDays(Timestamp timestamp, int days) throws ParseException  {
		Calendar calMas10 = Calendar.getInstance();
		Date date = timestamp;
		calMas10.setTime(date);
		calMas10.add(Calendar.DAY_OF_YEAR, days);
		java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String c = format.format(new java.sql.Date(calMas10.getTime().getTime()));		
		return new java.sql.Timestamp(format.parse(c).getTime());
	}
	
	public static Timestamp timestampAddHours(Timestamp timestamp, int hours) throws ParseException  {
		Calendar calMas10 = Calendar.getInstance();
		Date date = timestamp;
		calMas10.setTime(date);
		calMas10.add(Calendar.HOUR_OF_DAY, hours);
		java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String c = format.format(new java.sql.Date(calMas10.getTime().getTime()));		
		return new java.sql.Timestamp(format.parse(c).getTime());
	}
	
	public static Timestamp timestampAddSeconds(Timestamp timestamp, int seconds) throws ParseException  {
		Calendar calMas10 = Calendar.getInstance();
		Date date = timestamp;
		calMas10.setTime(date);
		calMas10.add(Calendar.SECOND, seconds);
		java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String c = format.format(new java.sql.Date(calMas10.getTime().getTime()));		
		return new java.sql.Timestamp(format.parse(c).getTime());
	}
	
	public static Timestamp getTimestampNow(){
		return new java.sql.Timestamp(Calendar.getInstance().getTime().getTime());
	}
	
	public static Date getJavaUtilDateNow(){
		return new Date(Calendar.getInstance().getTime().getTime());
	}
	
	public static java.sql.Date getJavaSqlDateNow(){
		return new java.sql.Date(Calendar.getInstance().getTime().getTime());
	}

}
